﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Utility
{
    public static class FileHelp
    {
        /// <summary>
        /// 改变上传文件名
        /// </summary>
        /// <param name="FileName">旧文件名</param>
        /// <returns>新文件名</returns>
        public static string FileVariant(string OldFileName)
        {
            string NewFileName = OldFileName.Substring(OldFileName.LastIndexOf('.'));
            NewFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond + NewFileName;
            return NewFileName;
        }

        /// <summary>
        /// 读取路径里的文件名部分
        /// </summary>
        /// <param name="Path"></param>
        public static string GetFileName(string Path)
        {
            return Path.Substring((Path.LastIndexOf('/') == -1) ? 0 : Path.LastIndexOf('/')+1);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="FileName"></param>
        public static void FileToHttp(string FileName)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode("Template.xls", System.Text.Encoding.Default));
            HttpContext.Current.Response.WriteFile(FileName);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 读取文件后缀，防止，用户上传木马
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public static string GetFilePostfix(string FileName)
        {
            return System.IO.Path.GetExtension(FileName).Trim('.');
            //return FileName.Substring(FileName.LastIndexOf(".")+1).ToLower();
        }

        public static string FileType = "jpg, gif, jpeg, png, htm, html, doc, rar, xls, pdf, css, txt, ppt, zip, docx, swf, txt";
    }
}
